<script type="text/javascript">
var enable_submit=true;
function submit_volume_create(isforce){
    var modal_selector = $("#volume_create_modal");
    var volume_name = modal_selector.find("#volume_name_cr");
    var capacity = modal_selector.find("#capacity_cr");
 	var qos = modal_selector.find("#volume_qos_dropdown")
 	var access_policy = modal_selector.find("#volume_isolation_dropdown")
    var protection_domain = modal_selector.find("#volume_PDs_select");
    var target_tier = modal_selector.find("#volume_tt_select");
    var snappolicy_dropdown = modal_selector.find('#snappolicy_dropdown')
 	var provisioning = modal_selector.find("input[name='disk_preparation']:checked").val();
    var chap_name = modal_selector.find("#chap_name");
    var chap_password = modal_selector.find("#chap_password");
    var analysis_latency = modal_selector.find(".analysis_latency:checked").val();
    var repnum = modal_selector.find("#repnum");
    var delete_code = modal_selector.find("#delete_code");
    var delete_code_r = modal_selector.find("#delete_code_r");
    var bValid = true;
    bValid = bValid && checkLength(volume_name, "名称", 1, 32 );
    bValid = bValid && checkRegexp( volume_name, /^[0-9a-z-]+$/, "名称应由小写英文字母、数字或中划线(英文)构成。");
    bValid = bValid && checkNull(capacity,'存储容量不能为空。');

    bValid = bValid && checkRegexp_num(capacity, /^[0-9]+$/i, "存储容量必须为大于0的整数");
    bValid = bValid && checkCapacity(capacity, 0, 32768, "存储容量不能大于32768。");
    
    if($.trim(chap_name.val()).length>0||$.trim(chap_password.val()).length>0)   {
        bValid = bValid && checkRegexp( chap_name, /^[0-9a-z-]+$/, "CHAP名称应由小写英文字母、数字、中划线(英文)构成。");
        bValid = bValid && checkCHAP(chap_name, chap_password);
    }

    var pool_id;
    {% if not pool %}
        pool_id = pool.attr('realvalue');
    {% else %}
        pool_id = "{{pool.id}}";
    {% endif %}


    if(repnum.is(":hidden")){
        repnum.attr("realvalue","");
    }
    if(delete_code.is(":hidden")){
        delete_code.attr("realvalue","");
        delete_code_r.attr("realvalue","");
    }
    if (bValid) {
        if (isforce){
            enable_submit = true;
        }
        if(enable_submit){       
            enable_submit=false;
            var $submitBtn = $('.modal-footer').find('.btn-blue');
            $submitBtn.html('<i class="fa fa-spinner fa-spin" style="margin:3px 6px;"></i>');
            $('.rtn-tip').slideUp('fast');
            var post_data = {
                "name" : "{{pool.name}}/"+volume_name.val(),    
                "username": "{{pool.username}}",
                "size_gb" : capacity.val(),
                "pool_id" : pool_id,
                "qos_id" : qos.attr('realvalue'),
                "access_policy_id": access_policy.attr('realvalue'),
                "protection_domain_id": protection_domain.attr("realvalue"),
                "provisioning": provisioning,
                "repnum": $.trim(repnum.attr("realvalue")),
                "priority": $.trim(target_tier.attr("realvalue")),
                "snappolicy_id": snappolicy_dropdown.attr("realvalue"),
                'chap_name': $.trim(chap_name.val()),
                'chap_password': $.trim(chap_password.val()),
                'analysis_latency': analysis_latency,
                "isforce": isforce
            }
            $.post("/volume/create", post_data, function(data){
                if (data.reply.is_success){
                    angular.element('#volume_controller').scope().close();
                    $('#volume_available_tb').trigger("reloadGrid")
                    enable_submit = true;                              
                } else {
                    error =  data.reply.error;
                    $('.rtn-tip').text(error).slideDown('fast');
                    $submitBtn.html(SelfTranslate("[[ 'label ok'|translate ]]"));
                    enable_submit = true;                           
                }
            });
        }
    }
}
$(function() {
    if ('{{pool.protocol}}' == 'nbd') {
        $('.user-info').hide();
    }

    /* 初始化页面的下拉列表 */
    var volume_isolation = [['','默认允许全部']];
    {% for isolation in isolations %}
        var values = new Array();
        values[0] = "{{isolation.id}}";
        values[1] = "{{isolation.name}}";
        volume_isolation.push(values);
    {% endfor %}
    var volume_qos = [['','默认无限制']];
    {% for qos in qoses %}
        var values = new Array();
        values[0] = "{{qos.id}}";
        values[1] = "{{qos.name}}";
        volume_qos.push(values);
    {% endfor %}

    var pool_PDs = [];
    {% for pd in protection_domains %}
        var values = new Array();
        values[0] = "{{ pd.id }}";
        values[1] = "{{ pd.name }}";
        pool_PDs.push(values)
    {% endfor %}
    var snappolicies = [['', '默认无自动快照']];
    {% for p in snappolicy %}
        var values = new Array();
        values[0] = "{{ p.id }}";
        values[1] = "{{ p.name }}";
        snappolicies.push(values)
    {% endfor %}

    $('#volume_isolation_dropdown').rqDropDown({
        position:true,
        "datatype":"local",
        "source":volume_isolation,  
    }); 
    $('#volume_qos_dropdown').rqDropDown({
        position:true,
        "datatype":"local",
        "source":volume_qos,    
    }); 
    $('#volume_PDs_select').rqDropDown({
        position:true,
        "datatype":"local",
        "source":pool_PDs   
    }); 
    $('#snappolicy_dropdown').rqDropDown({
        position:true,
        "datatype":"local",
        "source":snappolicies
    }); 
    $('#volume_create_modal #repnum').rqDropDown({
        position:true,
        "datatype":"local",
        "source":get_repnums()
    });
    $('#volume_create_modal #volume_tt_select').rqDropDown({
        position:true,
        "datatype":"local",
        "source":get_tiers()
    });
    var delete_code = [['2', '2'],['3', '3'],['4', '4'],['6', '6'],['8', '8'],['12', '12'],['16', '16']];
    $('#volume_create_modal #delete_code').rqDropDown({
        "datatype":"local",
        "source":delete_code
    });
    var delete_code_r = [['1', '1'],['2', '2'],['3', '3'],['4', '4']];
    $('#volume_create_modal #delete_code_r').rqDropDown({
        "datatype":"local",
        "source":delete_code_r
    });
    /* 初始化页面的下拉列表结束 */

    /* 为小图标绑定鼠标移入移出事件 */
    var obj = {
        'volume_name' : '由1至32位小写英文字母、数字、中划线(英文)构成。',
        'capacity': '存储容量必须为大于0的整数，且不能大于32768。',
        'chap_password': '可为空, 为空时将使用所属资源池的CHAP用户名称;由3至32位小写英文字母、数字、中划线构成。',
        'chap_name': '可为空, 为空时将使用所属资源池的CHAP用户名称;如果资源池未设置CHAP用户，则使用所属用户的CHAP用户;由3至32位小写英文字母、数字、中划线构成'
    }
    bindMouseEvent('#volume_create_modal',obj);
    /* 为小图标绑定鼠标移入移出事件结束 */
});


    
</script>
<div class="modal-header">  
    <h3>[['create volume'|translate]]</h3>  
</div> 
<div  class="modal-body modal-body-height" id="volume_create_modal">
<div class="volume-create-box">
<div class="rtn-tip"></div>
<table class="base-info">
    <tr>
        <td>
            <label>[['model volume name'|translate]]</label><a>*</a>
        </td>
        <td colspan="3">
            <input id="volume_name_cr" name="volume_name_cr" type="text"/>
            <i class="fa fa-question-circle" itype="volume_name"></i>
        </td>
    </tr>
    <tr>
        <td>
            <label>[['model storage capacity'|translate]]</label><a>*</a>
        </td>
        <td>
            <input id="capacity_cr" name="capacity_cr" max="10000" min="1" type="text" />
            <span class="unit">G</span>
            <i class="fa fa-question-circle" itype="capacity"></i>
        </td>
        <td>
            <label>[['Disk configuration'|translate]]</label><a>*</a>
        </td>
        <td>
            {% if pool.provisioning == 'thin' %}
            <label><input  type="radio" name="disk_preparation" value="thin" style="vertical-align:-3px;" checked="true"/>[['modal complex'|translate]]</label>
            <label><input  type="radio" name="disk_preparation" value="thick" style="vertical-align:-3px;" />[['modal streamline'|translate]]</label>
            {% else %}
             <label><input  type="radio" name="disk_preparation" value="thin" style="vertical-align:-3px;" />[['modal complex'|translate]]</label>
            <label><input  type="radio" name="disk_preparation" value="thick" style="vertical-align:-3px;"  checked="true"/>[['modal streamline'|translate]]</label>
            {% endif %}
        </td>
    </tr>
</table>

<table class="super-info">
    <tr>
        <td>
            <label>保护域</label>
        </td>
        <td>
            {% if protection_domains | length == 0 %}
            <input id="volume_PDs_select" readonly="readonly" class="rounded_dropdown" value='default' realvalue=""/>
            {% else %}
            <input id="volume_PDs_select" readonly="readonly" class="rounded_dropdown" value='{{ protection_domains[0].name }}' realvalue="{{ protection_domains[0].id }}"/>
            {% endif %}
        </td>
        <td>QoS</td>
        <td>
            <input id="volume_qos_dropdown" readonly="readonly" class="rounded_dropdown" value='默认无限制' realvalue=""/>
        </td>
    </tr>
    <tr>
        <td>
            {% if pool.ec_data %}
            <label>纠删码</label>
            {% else %}
            <label>副本数</label>
            {% endif %}
        </td>
        <td>
            {% if pool.ec_data %}
            <input id=""  style="width:40%;" readonly="readonly" class="rounded_dropdown" value='{{pool.ec_data}}' realvalue="2"/>
            <b>+</b>
            <input id=""  style="width:40%;" readonly="readonly" class="rounded_dropdown" value='{{pool.ec_code}}' realvalue="1"/>
            {% else %}
            <input id="repnum" readonly="readonly" class="rounded_dropdown" value='{{pool.repnum}}' realvalue="{{pool.repnum}}"/>
            {% endif %}
        </td>
        <td>
            <label>目标分层</label>
        </td>
        <td>
            <input id="volume_tt_select" readonly="readonly" class="rounded_dropdown" value="[['{{pool.priority}}'|translate]]" realvalue="{{pool.priority}}"/>
        </td>
    </tr>
    <tr>
        <td>
            <label>访问策略</label>
        </td>
        <td>
            <input id="volume_isolation_dropdown" readonly="readonly" class="rounded_dropdown" value='默认允许全部' realvalue=""/>
        </td>
        <td>
            <label>自动快照</label>
        </td>
        <td>
            <input id="snappolicy_dropdown" readonly="readonly" class="rounded_dropdown"  value='默认无自动快照' realvalue=""/>
        </td>
    </tr>
    </table>
    <table class="switch-info">
    <tr>
        <td>
            <label>Latency监控开关</label>
        </td>
        <td>
            <label><input type="radio" class="analysis_latency" name="analysis_latency" value="" style="vertical-align:-3px;" checked="true"/>关闭</label>
            <label><input type="radio" class="analysis_latency" name="analysis_latency" value="1000" style="vertical-align:-3px;" />开启</label>
        </td>
    </tr>
</table>
<table class="user-info">
    <tr>
        <td>
            <label>[["label chap name"|translate]]</label>
        </td>
        <td>
            <input type="text" name="chap_name" id="chap_name" />
            <i class="fa fa-question-circle" itype="chap_name"></i>
        </td>
        <td>
            <label>[["label chap password"|translate]]</label>
        </td>
        <td>
            <input type="password" name="chap_password" id="chap_password" readonly onfocus="this.removeAttribute('readonly');" onpaste="return false" oncontextmenu="window.event.returnValue=false" />
            <i class="fa fa-eye" style="right:60px"></i>
            <i class="fa fa-question-circle" itype="chap_password"></i>
        </td>
    </tr>
</table>
</div>
<div class="error-tip">
    <p></p><span></span>
</div>
<div class="tip">
    <p></p><span></span>
</div>
</div>
<div class="modal-footer">  
    <button class="btn btn-blue" ng-click="ok()">[[ 'label ok'|translate ]]</button>  
    <button class="btn btn-white" ng-click="cancel()">[[ 'label cancel'|translate ]]</button>  
</div>
